package cn.lecosa.spark.es

import org.apache.spark.sql.SQLContext
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object schemaTest {
    def main(args:Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName("RDD2DataFrameByProgrammaticallyScala") //设置应用程序的名称，在程序运行的监控界面可以看到名称
    conf.setMaster("local")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    val people = sc.textFile("F:/spark/workspace2/bigdata/data/person",3)  
//    val people = sc.textFile("C://Users//DS01//Desktop//persons.txt")
    val schemaString = "name age"
    import org.apache.spark.sql.Row;
    import org.apache.spark.sql.types.{StructType,StructField,StringType};
    
    val schema = StructType(schemaString.split(" ").map(fieldName=>StructField(fieldName,StringType,true)))
    val rowRDD = people.map(_.split(",")).map(p=>Row(p(0),p(1).trim))
    val peopleDataFrame = sqlContext.createDataFrame(rowRDD,schema)
    peopleDataFrame.registerTempTable("people")
//    val results = sqlContext.sql("select name from people")
    peopleDataFrame.show()
//    results.map(t=>"Name: "+t(0)).collect().foreach(println)
  }
}